﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DO.Clustering;

namespace BLL.Clustering
{
    public class TermWeightManager
    {
        public static Dictionary<int, double> GetTfidfScores(
            Dictionary<int, Dictionary<int, int>> projTermFreqs,
            Dictionary<int, string> terms)
        {
            Dictionary<int, double> termWeights = new Dictionary<int, double>();
            foreach (int termID in terms.Keys)
            {
                int docContainingTerm = 0;
                double tf = 0;
                foreach (int docId in projTermFreqs.Keys)
                {
                    Dictionary<int, int> termFreqs = projTermFreqs[docId];
                    if (termFreqs.ContainsKey(termID))
                    {
                        docContainingTerm++;
                        int wordCountInDoc = 0;
                        foreach (int termFreq in termFreqs.Values)
                            wordCountInDoc += termFreq;
                        tf += ((double)termFreqs[termID]) / wordCountInDoc;
                    }
                }
                double idf = Math.Log(((double)projTermFreqs.Count) / docContainingTerm);
                termWeights.Add(termID, tf * idf);
            }
            return termWeights;
        }

        public static Dictionary<int, Dictionary<int, FeatureFreq>> BuildFeatureFrequencies(
            Dictionary<int,Dictionary<int,int>> docTermFreqs,
            Dictionary<int,double> termWeights)
        {
            Dictionary<int, Dictionary<int, FeatureFreq>> docFeatureFreq = new Dictionary<int, Dictionary<int, FeatureFreq>>();
            foreach (int projID in docTermFreqs.Keys)
            {
                Dictionary<int, FeatureFreq> featureFreqs = new Dictionary<int, FeatureFreq>();
                foreach (int termID in docTermFreqs[projID].Keys)
                {
                    FeatureFreq feat = new FeatureFreq();
                    feat.FeatureID = termID;
                    feat.Weight = termWeights.ContainsKey(termID) ? termWeights[termID] : 1;
                    feat.Count = docTermFreqs[projID][termID];
                    featureFreqs.Add(termID, feat);
                }
                docFeatureFreq.Add(projID, featureFreqs);
            }
            return docFeatureFreq;
        }

        public static List<int> GetTopTerms(Dictionary<int,double> termWeights, int top)
        {
            List<Feature> feats=new List<Feature>();
            foreach(int termID in termWeights.Keys)
            {
                Feature feat=new Feature()
                                 {
                                     FeatureID=termID,
                                     Weight=termWeights[termID]
                                 };
                feats.Add(feat);
            }
            feats.Sort();
            List<int> topTermIDs=new List<int>();
            foreach(Feature feat in feats)
            {
                topTermIDs.Add(feat.FeatureID);
                if(topTermIDs.Count>=top)
                    break;
            }
            return topTermIDs;
        }
    }
}
